/* Author: Keir Kettle

*/
$(function() {
	$("#redactor").redactor();
	
  	$.get("/ajax/titles", function(d) {
		if (d.length)
		{
			var treats = $.parseJSON(d), ul = $("<ul />");					
			for (i in treats)
			{
				var t = treats[i], li = $("<li />").addClass("id_" + t.key + " span2 boxy");
				if (!t.title.length)
				{
					t.title = "An untitled treat!";
				}
				$("<a />").attr("href", t.key).text(t.title).bind("click", function(e) {
		  			e.preventDefault();
			  		$.get("/ajax/id/" + $(this).attr("href"), function(d) {

		  				var treat = $.parseJSON(d); 
		  				$("#title").val((treat.title.length ? treat.title : "")); 
		  				$("#id").val(treat.key);
		  				$("#redactor").setCode(treat.page.replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g,'"').replace(/&#39;/g,"'")); 

		  			});
		  			console.log($(this), $(this).parent("li"));
		  			$(this).parents("ul").find("li").css("backgroundColor", "white").end().end().parent("li").css("backgroundColor", "#ededed");
			  	}).appendTo(li);
				$("<span />").html(t.date).appendTo(li);  
				li.appendTo(ul);
			}
			$("div.treats").append(ul);
		}
		else
		{
			$("div.treats").append("<p>The pouch is empty!?</p>");
		}
		$("img.ajax", "div.treats").addClass("hidden");
  	})


	$("#save").bind("click", function(e) {
		e.preventDefault(); 
		$(this).next("img.ajax").show(); 	
	 	save();
		$(this).next("img.ajax").hide();
	});
});


function save() {
	
		var method = $("#id").val().length ? "update" : "save";
		var data =  "page=" + $('#redactor').getCode() + "&title=" + $("#title").val(); 
		if (method == "update")
		{
			data += "&treat_key=" + $("#id").val()
		}

		$.ajax({
			type: "POST",
			url: "/ajax/" + method, 
			data: data, 
			success: function(result, method){
				if (result.length > 0)
				{
					if (result == "OK")
					{
						
						$("span.saved").removeClass("hidden").delay(3000).queue(function() {
					        $(this).addClass("hidden"); 
					        $(this).dequeue(); //Prevents box from holding color with no fadeOut on second click.
					    }); 
						$("li.id_" + $("#id").val()).find("a").text($("#title").val()).end().find("span").html("Just now!").end()
									.css("-webkit-transition","all 3s ease")
								    .css("backgroundColor","#ededed")
								    .css("-moz-transition","all 3s ease")
								    .css("-o-transition","all 3s ease")
								    .css("-ms-transition","all 3s ease")
								    /* Avoiding having to use a jQ plugin. */

								    .css("backgroundColor","#ffff99").delay(3000).queue(function() {
								        $(this).css("backgroundColor","#ededed"); 
								        $(this).dequeue(); //Prevents box from holding color with no fadeOut on second click.
								    }); 
					}
					else
					{
						var li = $("<li />").addClass("id_" + result + " span2 boxy"); 
						$("<a />").attr("href", result).text($("#title").val()).bind("click", function(e) {
				  			e.preventDefault();
					  		$.get("/ajax/id/" + $(this).attr("href"), function(d) {
				  				var treat = $.parseJSON(d); 
				  				$("#title").val((treat.title.length ? treat.title : "")); 
				  				$("#id").val(treat.key);
				  				$("#redactor").setCode(treat.page); 
				  			});
					  	}).appendTo(li);	
						$("div.treats ul").append(li);
						$("form")[0].reset();
						$('#redactor').setCode("");
						
					}
				}
			}
		});
};
